﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.API.Application.Command.LeaveCommand;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service;
using System.Security.Claims;
using MediatR;
using Microsoft.AspNetCore.Authorization;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 请假控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class LeaveController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<LeaveController> logger;
        /// <summary>
        /// 请假服务层
        /// </summary>
        ILeaveService lSer;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mMediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="lSer">请假服务层</param>
        /// <param name="mMediator">中介者</param>
        public LeaveController(ILogger<LeaveController> logger, ILeaveService lSer, IMediator mMediator)
        {
            this.logger = logger;
            this.lSer = lSer;
            this.mMediator = mMediator;
        }
        /// <summary>
        /// 反填请假人
        /// </summary>
        /// <param name="request">用户编号</param>
        /// <returns>返回数据集合</returns>
        [HttpGet]
        public async Task<ApiResult<UserInfo>> GetUser([FromQuery]LeaveBackillUserCommand request)
        {
            ApiResult<UserInfo> result = new ApiResult<UserInfo>();
            try
            {
                request.Uid =Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);

                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 反填请假人" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示部门领导
        /// </summary> Leavedepartment
        /// <param name="request">部门</param> 
        /// <returns>返回数据集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<UserInfo>>> Getdepartment([FromQuery] LeavedepartmentCommand request)
        {
            ApiResult<List<UserInfo>> result = new ApiResult<List<UserInfo>>();
            try
            {

                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 显示部门领导" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加请假单
        /// </summary>
        /// <param name="request">要添加的数量</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddLeave(LeaveUpdateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 添加请假单" + ex.Message);
                throw;
            }
            return result;
        }


        /// <summary>
        /// 分页显示请假列表
        /// </summary>
        /// <param name="request">中介者</param>
        /// <returns>返回数据信息</returns>
        [HttpGet]
        public async Task<ApiResult<FenYeDTO<LeaveDTo>>> GetAllLeave([FromQuery] LeaveShowCommand request)
        {
            ApiResult<FenYeDTO<LeaveDTo>> result = new ApiResult<FenYeDTO<LeaveDTo>>();
            try
            {
                request.Uid= Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 分页显示请假列表" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填申请信息
        /// </summary>
        /// <param name="request">申请编号</param>
        /// <returns>返回数据信息</returns>
        [HttpGet]
        public async Task<ApiResult<Leave>> GetLeaveinfo([FromQuery]LeaveBackillCommand request)
        {
            ApiResult<Leave> result = new ApiResult<Leave>();
            try
            {
                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 反填申请信息" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="request">修改后的数据信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UPdLeave(LeaveUpdateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code =await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 修改信息" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 审计
        /// </summary>
        /// <param name="request">申请编号</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> LeaveSJ([FromQuery]LeaveSJCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 审计" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request">申请编号</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> LeaveDel([FromQuery] LeaveDeleteCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【请假控制器】 删除" + ex.Message);
                throw;
            }
            return result;
        }

    }
}
